import * as Sequelize from "sequelize";
import { Application } from "egg";

interface RoleUserAttributes {
    id: string;
    role_id: string;
    user_id: string;
    create_user_id: string;
    modify_user_id: string;
    create_time: string;
    modify_time: string;
}

type RoleUserInstance = Sequelize.Instance<RoleUserAttributes> & RoleUserAttributes;

export default (app: Application) => {
    const { UUID, UUIDV4, DATE,NOW } = app.Sequelize;
    const attributes: SequelizeAttributes<RoleUserAttributes> = {
        id: {
            type: UUID,
            primaryKey: true,
            allowNull: false,
            defaultValue: UUIDV4
        },
        role_id: { type: UUID, allowNull: false},
        user_id: { type: UUID, allowNull: false},
        create_user_id: { type: UUID, allowNull: false },
        modify_user_id: { type: UUID, allowNull: false },
        create_time: { type: DATE, allowNull: false ,defaultValue: NOW},
        modify_time: { type: DATE, allowNull: false ,defaultValue: NOW}
    };
    const model = app.model.define<RoleUserInstance, RoleUserAttributes>("RoleUser", attributes,{
        tableName: 't_role_user'
    });
    model.associate = ()=>{
        app.model.models.RoleUser.belongsTo(app.model.models.Role,{foreignKey:"role_id", targetKey:"id"})
        app.model.models.RoleUser.belongsTo(app.model.models.User,{foreignKey:"user_id", targetKey:"id"})
    }
    return model
};